package com.Txunda.fxw.ui.activity.law;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.http.Office;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Map;

/**
 * 律师事务所的详情
 *
 * @author Administrator
 */
public class SingleLawDetailAty extends BaseAty {
    @ViewInject(R.id.aty_title3_tv_center)
    private TextView aty_title3_tv_center;
    @ViewInject(R.id.aty_title3_tv_right2)
    private TextView aty_title3_tv_right2;
    @ViewInject(R.id.law_list_lv)
    private ListView law_list_lv;
    private Office office;
    private String district_name;
    private ArrayList<Map<String, String>> list_data;
    private MyAdapter adapter;
    private Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String area_name = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_mode);
        district_name = getIntent().getStringExtra("district_name");
        String district_id3 = getIntent().getStringExtra("district_id3");
        if (district_name == null || district_name.equals("")) {
            aty_title3_tv_right2.setText(area_name);
            showProgressDialog();
            office.officelist(district_id3, "0", area_name, this);
        } else {
            aty_title3_tv_right2.setText(district_name);
            showProgressDialog();
            if (district_id3.equals("121")) {
                office.officelist(district_id3, "0", district_name, this);
            } else {
                office.officelist(district_id3, "1", district_name, this);
            }
        }

        law_list_lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String o_id = list_data.get(position).get("o_id");
                bundle = new Bundle();
                bundle.putString("district_name", district_name);
                bundle.putString("district_id", o_id);
                startActivity(Law_firmAty.class, bundle);
            }
        });

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_single_law_detail;
    }

    @Override
    protected void initialized() {
        office = new Office();
        aty_title3_tv_right2.setVisibility(View.VISIBLE);
        aty_title3_tv_center.setVisibility(View.VISIBLE);
        aty_title3_tv_center.setText("律师事务所");
        adapter = new MyAdapter();
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(map))
            return;
        if (requestUrl.contains("officelist")) {
            list_data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
            law_list_lv.setAdapter(adapter);
        }
    }

    @Override
    protected void requestData() {

    }

    /**
     * 律师列表adapter
     *
     * @author Administrator
     */
    public class MyAdapter extends BaseAdapter {

        MyViewHolder vh;

        @Override
        public int getCount() {
            int ret = 0;
            if (list_data != null) {
                ret = list_data.size();
            }
            return ret;
        }

        @Override
        public Object getItem(int position) {
            return list_data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(SingleLawDetailAty.this).inflate(R.layout.item_company_list, null);
                vh = new MyViewHolder();
                ViewUtils.inject(vh, convertView);
                convertView.setTag(vh);
            } else {
                vh = (MyViewHolder) convertView.getTag();
            }
            Glide.with(SingleLawDetailAty.this).load(list_data.get(position).get("cover_pic")).into(vh.item_company_img);
            vh.item_company_title.setText(list_data.get(position).get("name"));

            return convertView;
        }

        class MyViewHolder {
            @ViewInject(R.id.compmay_frame_item)
            private FrameLayout compmay_frame_item;
            @ViewInject(R.id.item_company_img)
            private ImageView item_company_img;
            @ViewInject(R.id.item_company_title)
            private TextView item_company_title;
            @ViewInject(R.id.item_company_distance)
            private TextView item_company_distance;
            @ViewInject(R.id.item_company_address)
            private TextView item_company_address;
        }

    }

}
